﻿using DataAccess.HomeEquityLoan;
using FOModels.HomeEquityLoan;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Bussiness.HomeEquityLoan
{
    public class HomeEquityLoanBLL
    {
        private HomeEquityLoanDAL dal;
        public HomeEquityLoanBLL()
        {
            dal = new HomeEquityLoanDAL();
        }
        public List<HomeEquityLoanApplyModels> getcontract()
        {
            List<HomeEquityLoanApplyModels> list = new List<HomeEquityLoanApplyModels>();
            HomeEquityLoanApplyModels fur;
            DataTable tb = dal.getcontract();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanApplyModels();
                    fur.HomeEquityLoanApplyID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.HomeEquityLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<HomeEquityLoanApplyModels> getcontractbystatuscount(int statusid)
        {
            List<HomeEquityLoanApplyModels> list = new List<HomeEquityLoanApplyModels>();
            HomeEquityLoanApplyModels fur;
            DataTable tb = dal.getcontractbystatuscount(statusid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanApplyModels();
                    fur.HomeEquityLoanApplyID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.HomeEquityLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public HomeEquityLoanApplyModels getcontractbyid(int HomeEquityLoanapplyid)
        {

            HomeEquityLoanApplyModels fur = new HomeEquityLoanApplyModels();
            DataTable tb = dal.getcontractbyid(HomeEquityLoanapplyid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanApplyModels();
                    fur.HomeEquityLoanApplyID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.HomeEquityLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();

                }
            }
            return fur;
        }
        public int HomeEquityLoanApply(int HomeEquityLoanID, HomeEquityLoanApplyModels HomeEquityLoan)
        {
            if (dal.HomeEquityLoanApply(HomeEquityLoanID, HomeEquityLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteHomeEquityLoanApply(int HomeEquityLoanApplyID)
        {
            if (dal.DeleteHomeEquityLoanApply(HomeEquityLoanApplyID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int UpdateStatus(HomeEquityLoanApplyModels HomeEquityLoan)
        {
            if (dal.UpdateStatus(HomeEquityLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public HomeEquityLoanModels getTOP1()
        {

            HomeEquityLoanModels fur = new HomeEquityLoanModels();
            DataTable tb = dal.getTOP1();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                }
                return fur;
            }
            else
                return null;
        }
        public List<HomeEquityLoanModels> getall(int langid)
        {
            List<HomeEquityLoanModels> list = new List<HomeEquityLoanModels>();
            HomeEquityLoanModels fur;
            DataTable tb = dal.getall(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur = new HomeEquityLoanModels();
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = row[3].ToString();
                    fur.MaximumBalance = row[4].ToString();
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.FlagImage = row[27].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public HomeEquityLoanModels getHomeEquityLoanByID(int HomeEquityLoanID, int langid)
        {

            HomeEquityLoanModels fur = new HomeEquityLoanModels();
            DataTable tb = dal.getHomeEquityLoanByID(HomeEquityLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public HomeEquityLoanModels getHomeEquityLoanByIDManager(int HomeEquityLoanID, int langid)
        {

            HomeEquityLoanModels fur = new HomeEquityLoanModels();
            DataTable tb = dal.getHomeEquityLoanByIDManager(HomeEquityLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public List<HomeEquityLoanModels> getHomeEquityLoanDefault(int langid)
        {
            List<HomeEquityLoanModels> list = new List<HomeEquityLoanModels>();
            HomeEquityLoanModels fur;
            DataTable tb = dal.getHomeEquityLoanDefault(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanModels();
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<HomeEquityLoanModels> getHomeEquityLoanDefaultTEst(int langid)
        {
            List<HomeEquityLoanModels> list = new List<HomeEquityLoanModels>();
            HomeEquityLoanModels fur;
            DataTable tb = dal.getHomeEquityLoanDefaultTest(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanModels();
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<HomeEquityLoanModels> getHomeEquityLoanrelati(int CompanyID, int HomeLoanID, int langid)
        {
            List<HomeEquityLoanModels> list = new List<HomeEquityLoanModels>();
            HomeEquityLoanModels fur;
            DataTable tb = dal.getHomeEquityLoanrelati(CompanyID, HomeLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanModels();
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<HomeEquityLoanModels> getHomeEquityLoan(int planid, string balance, int years, int langid)
        {
            List<HomeEquityLoanModels> list = new List<HomeEquityLoanModels>();
            HomeEquityLoanModels fur;
            DataTable tb = dal.getHomeEquityLoan(planid, balance, years, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanModels();
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<HomeEquityLoanModels> getHomeEquityLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            List<HomeEquityLoanModels> list = new List<HomeEquityLoanModels>();
            HomeEquityLoanModels fur;
            DataTable tb = dal.getHomeEquityLoanByProvider(planid, balance, years, CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanModels();
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<HomeEquityLoanModels> getHomeEquityLoanDefaultByProvider(int CompanyID, int langid)
        {
            List<HomeEquityLoanModels> list = new List<HomeEquityLoanModels>();
            HomeEquityLoanModels fur;
            DataTable tb = dal.getHomeEquityLoanDefaultByProvider(CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new HomeEquityLoanModels();
                    fur.HomeEquityLoanID = int.Parse(row[0].ToString());
                    fur.HomeEquityLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public int UpdateHomeEquityLoan(HomeEquityLoanModels models, int langid)
        {
            if (dal.UpdateHomeEquityLoan(models) > 0 && dal.UpdateHomeEquityLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertHomeEquityLoan(HomeEquityLoanModels models)
        {
            if (dal.InsertHomeEquityLoan(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertHomeEquityLoanLang(HomeEquityLoanModels models, int langid)
        {
            if (dal.InsertHomeEquityLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteHomeEquityLoan(int CarLoanID)
        {
            if (dal.DeleteHomeEquityLoanLang(CarLoanID) > 0 && dal.DeleteHomeEquityLoan(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
